﻿using Intex.Tools;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using INTEXTECH;
using ReflectionStudio.Controls;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Data.SqlClient;
using System.IO;
namespace WPFExcelReport
{
    public partial class JLTCAD2ITXData : BaseFunction
    {
        private VWTSAER _VWT;
        public VWTSAER VWT
        {
            get
            {
                if (_VWT == null)
                    _VWT = new VWTSAER();
                return _VWT;
            }
        }
        public bool IsAutoRun = false;
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            }
        }
        INTEXBL INTEXBL
        {
            get
            {
                return app.INTEXBL;
            }
        }
        INTEXDBC INTEXDBC
        {
            get
            {
                return app.INTEXDBC;
            }
        }
        JLTCAD2INTEX m_wintex;
        GB2312Convert8859P1 gb2312 = new GB2312Convert8859P1();
        public string ITXMANDANT = "00", ITXUSER = "INTEXMAP", ITXDATE = "20120723";
        public string ITXLOOMTYPE = "T610  ";
        public string ITXFART = "                    ", ITXGART = "                    ", ITXBART = "                    ";
        public string ITXFVAR = "  ", ITXGVAR = "  ", ITXBVAR = "1 ";
        public string ITXFCOLOR = "          ", ITXGCOLOR = "          ", ITXBCOLOR = "          ";
        public string ITXFFINISH = "          ", ITXCADID = "1";
        public string ITXFLEN = "1", ITXFUNIT = "M", ITXGLEN = "1", ITXBLEN = "1", ITXFGRATIO = "1", ITXGBRATIO = "1";
        SETINTEXDB INTEXDB = new SETINTEXDB();
        public DataSet SetDISPO;
        public SETINTEXDB SetData = new SETINTEXDB();
        public DataSet Set4ITX = new DataSet();
        public string CADCHAR = " ", ITXCHAR = " ";
        DataRow dwIntexStd = null;
        public Dictionary<string, object> CADTable = new Dictionary<string, object>();
        private Convert2Intex Convert2Intex;
        public DataSet SetCAD;
        bool IsRun = false;
        Queue<DataSet> JobQueue;
        BackgroundWorker bgWorker;
        public DataSet SetJob;
        DataSet SetEXT = new DataSet();
        public JLTCAD2ITXData()
            : base()
        {
            if (SetCAD == null)
            {
                SetCAD = new SETINTEXDB();
            }
            JobQueue = new Queue<DataSet>();
            bgWorker = new BackgroundWorker();
            bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
            bgWorker.RunWorkerCompleted += bgWorker_RunWorkerCompleted;
        }
 
        #region=============INTEX WS/BS=============================
        void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            IsRun = false;
            EventDispatcher.Instance.RaiseStatus("停止自动转换", StatusEventType.StopProgress);
        }
        void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            if (!IsRun)
            {
                IsRun = true;
                EventDispatcher.Instance.RaiseStatus("开始自动转换", StatusEventType.StopProgress);
            }
            AutoRun();
        }
        public DataSet ResetSetDispo(string setID = "")
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            DataSet settemp;
            try
            {
                ArrayList tables = new ArrayList();
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\JLTPRODSETUP.xls");
                if (!System.IO.File.Exists(filePath))
                {
                    return setReturn;
                }
                if (exAgent == null)
                {
                    exAgent = new ExcelX.ExcelLib.ExcelAgent(filePath);
                }
                setResult = exAgent.LoadDataTableDef(filePath, "");
                filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\JLTPRODSETUP.XML");
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }
                setResult.WriteXml(filePath);
                setResult = new DataSet();
                setResult = LoadSetDispo(setID);
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadSetDispo(string setID = "")
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\JLTPRODSETUP.XML");
                if (!System.IO.File.Exists(filePath))
                {
                    return setReturn;
                }
                setResult.ReadXml(filePath);
                setReturn.Merge(setResult);
                setResult = LoadTechArticle();
                setReturn.Merge(setResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return setReturn;
        }
        public DataSet LoadTechArticle()
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataTable tableLocal = null;
            SqlCommand sql;
            try
            {

                sql = SqlServerDB.CreateCommand();
                sql.CommandText = "SELECT 编号 as FID,名称 as FName FROM  品名";

                SResult =INTEXDBC.Connect2Proxy(sql);
                if (SResult.Tables.Count > 0)
                {
                    tableLocal = SResult.Tables[0];
                    tableLocal.TableName = "TechArticle";
                    SReturn.Merge(SResult);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public void SaveSetDispo(DataTable tbDispo)
        {
            DataSet setDispo = new DataSet();

            try
            {
                setDispo.Merge(tbDispo);
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\Interface\DATADISPO.XML");
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                    setDispo.WriteXml(filePath);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        #endregion

        private bool INTEX_EXISTS(string art, string var, string color, string finish)
        {
            bool exist = false;
            try
            {
                DataSet setReturn = new DataSet();
                DataSet setResult = new DataSet();
                DataTable tbLocal = null;
                OracleCommand or1;
                or1 = app.OracleDB.CreateCommand();
                or1.CommandText = "SELECT count(*) FROM ABPOS"
               + " WHERE ABPART=:ABPART AND ABPVAR=:ABPVAR AND ABPFARBE=:ABPFARBE AND ABPBESCHICH=:ABPBESCHICH";
                or1.Parameters.Add("ABPART", OracleType.NVarChar).Value = art;
                or1.Parameters.Add("ABPVAR", OracleType.NVarChar).Value = var;
                or1.Parameters.Add("ABPFARBE", OracleType.NVarChar).Value = color;
                or1.Parameters.Add("ABPBESCHICH", OracleType.NVarChar).Value = finish;

                setResult = INTEXDBC.Connect2Proxy(or1);
                if (setResult.Tables.Count > 0)
                {
                    tbLocal = setResult.Tables[0];
                    if (tbLocal.Rows.Count > 0)
                    {
                        if (System.Convert.ToInt64(tbLocal.Rows[0][0].ToString()) > 0)
                        {
                            exist = true;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return exist;
        }
        public void ConvertToIntex()
        {
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            string art, var, color, finish;
            string stage;
            string matchid = "";
            string yart, yvar, ycolor, ydesc;
            string apkrs = "", aplfd = "", apdes = "";
            DataSet setFlow = new DataSet();
            DataTable tbGSCOLOR = new DataTable();
            string expression = "";
            DataRow[] rows;
            try
            {
                string _CADPATTERN = "";
                Set4ITX.Clear();
                SetEXT = new DataSet();
                SetEXT.Merge(SetCAD);
                SetEXT.Merge(SetJob);
                Convert2Intex = new Convert2Intex(SetEXT, dwIntexStd, OracleDB, proxy);
                dwIntexStd = Convert2Intex.dwIntexStd;
                if (Convert.ToDouble(dwIntexStd["ITXBRATIO"].ToString().Trim())==0)
                {
                    dwIntexStd["ITXBRATIO"] = "1";
                }
                if (Convert.ToDouble(dwIntexStd["ITXWRATIO"].ToString().Trim()) == 0) 
                {
                    dwIntexStd["ITXWRATIO"] = "1";
                }
                if (Convert.ToDouble(dwIntexStd["ITXFGRATIO"].ToString().Trim()) == 0)
             
                {
                    dwIntexStd["ITXFGRATIO"] = "10000";
                }
                if (Convert.ToDouble(dwIntexStd["ITXGBRATIO"].ToString().Trim()) == 0)
             
                {
                    dwIntexStd["ITXGBRATIO"] = "1000";
                } 
                art = dwIntexStd["ITXFART"].ToString();
                var = dwIntexStd["ITXFVAR"].ToString();
                color = dwIntexStd["ITXFCOLOR"].ToString();
                finish = dwIntexStd["ITXFFINISH"].ToString();
                Set4ITX.Merge(SetEXT.Tables["INTEXMAP"]);
                // ITXFART ITXFVAR  ITXFCOLOR ITXFFINISH
                if (INTEX_EXISTS(art, var, color, finish))
                {
                    dwIntexStd["ITXCONTROL"] = "05";
                    //return;
                }

                //setReturn.Merge(Convert2Intex.SetExt);

                #region=======VKPOS===========
                //找到所有的VA10，对应的VKPOS，创建WDECK
                DataRow dwData = INTEXDB.VAFARBEN.NewRow();
                dwData["VAFMANDANT"] = "00";
                dwData["VAFART"] = Left(art + "                ", 24);
                dwData["VAFFARBE"] = Left(color + "                ", 10);
                DataSet SVA10 = INTEXDBC.QueryVAFARBEN(dwData, 4);
                foreach (DataRow rx in SVA10.Tables[0].Rows)
                {
                    rx["VAFPKUNDNR"] = "00000000";
                    rx["VAFPART"] = Left(art + "               ", 20);
                }
                SVA10.Tables[0].TableName = "VAFARBEN";
                if (Convert2Intex.SetExt.Tables.Contains("VAFARBEN"))
                {
                    Convert2Intex.SetExt.Tables["VAFARBEN"].Clear();
                }
                Set4ITX.Merge(SVA10);
                Convert2Intex.SetExt.Merge(SVA10);

                dwData = INTEXDB.VAART.NewRow();
                dwData["VAAMANDANT"] = "00";
                dwData["VAAART"] = Left(art + "                 ", 24);
                setResult = INTEXDBC.QueryVAART(dwData, 2);
                setResult.Tables[0].TableName = "VAART";
                foreach (DataRow rx in setResult.Tables[0].Rows)
                {
                    rx["VAADART"] = Left(art + "               ", 20);
                    rx["VAADKUNDNR"] = "00000000";
                    rx["VAALO"] = "  ";
                    rx["VAABESTANDF"] = "J";
                }

                Set4ITX.Merge(setResult);

                dwData = INTEXDB.VKPOS.NewRow();
                dwData["VKPMANDANT"] = "00";
                dwData["VKPART"] = Left(art + "                ", 24);
                dwData["VKPFARBE"] = Left(color + "                ", 10);
                DataSet SVK05 = INTEXDBC.QueryVKPOS(dwData, 5);
                foreach (DataRow dwVK05 in SVK05.Tables[0].Rows)
                {
                    dwVK05["VKPPRODST"] = "M";
                    setResult = Convert2WDECK(dwVK05);
                    Set4ITX.Merge(setResult);
                }
                Set4ITX.Merge(SVK05);
                #endregion
                #region======FABRIC================

                setResult = Convert2WASTAMM();
                Set4ITX.Merge(setResult);
                setResult = Convert2WTSCHUSS();
                Set4ITX.Merge(setResult);
                setResult = Convert2WAFARBEN();
                Set4ITX.Merge(setResult);
                setResult = Convert2WAFINISH();
                setResult.Tables[0].TableName = "WAFARBEN";
                Set4ITX.Merge(setResult);
                setResult = Convert2WABIND();
                Set4ITX.Merge(setResult);
                setResult = Convert2WTEINZ();
                Set4ITX.Merge(setResult);

                setResult = Convert2WTGEW();
                Set4ITX.Merge(setResult);
                setResult = Convert2WASTPASS();
                Set4ITX.Merge(setResult);
                setResult = Convert2WTPASS();
                Set4ITX.Merge(setResult);

                setResult = Convert2WASTKE();
                Set4ITX.Merge(setResult);
                setResult = Convert2WASTGA();
                Set4ITX.Merge(setResult);
                setTemp = new DataSet();

                foreach (DataRow dwMaster in Set4ITX.Tables["WASTGA"].Rows)
                {
                    yart = dwMaster["WAGGART"].ToString();
                    yvar = dwMaster["WAGGVAR"].ToString();
                    ycolor = dwMaster["WAGGFARBE"].ToString();
                    ydesc = dwMaster["WAGBEM"].ToString();
                    setResult = Convert2GSCOLOR(yart, yvar, ycolor, ydesc);
                    setTemp.Merge(setResult);
                }
                Set4ITX.Merge(setTemp);
                stage = "F";
                setTemp = new DataSet();
                foreach (DataRow dwMaster in Set4ITX.Tables["WASTAMM"].Rows)
                {
                    art = dwMaster["WAART"].ToString();
                    var = dwMaster["WAVAR"].ToString();
                    matchid = finish;
                    setFlow = new DataSet();
                    setFlow = GetProcessFlow(stage, art, var, color, matchid);
                    if (setFlow.Tables.Count == 0)
                        continue;
                    if (setFlow.Tables[0].Rows.Count == 0)
                        continue;
                    foreach (DataRow dwFlow in setFlow.Tables[0].Rows)
                    {
                        setResult = Convert2APZUORD(stage, art, var, dwFlow);
                        setTemp.Merge(setResult);
                    }
                }
                Set4ITX.Merge(setTemp);
                stage = "R";
                setTemp = new DataSet();
                foreach (DataRow dwMaster in Set4ITX.Tables["WASTAMM"].Rows)
                {
                    art = dwMaster["WAVWART"].ToString();
                    var = dwMaster["WAVWVAR"].ToString();
                    matchid = dwMaster["WARABLOM"].ToString().Trim(); //织机类型代码
                    setFlow = new DataSet();
                    setFlow = GetProcessFlow(stage, art, var, color, "");
                    if (setFlow.Tables.Count == 0)
                        continue;
                    if (setFlow.Tables[0].Rows.Count == 0)
                        continue;
                    foreach (DataRow dwFlow in setFlow.Tables[0].Rows)
                    {
                        setResult = Convert2APZUORD(stage, art, var, dwFlow);
                        setTemp.Merge(setResult);
                    }
                }
                Set4ITX.Merge(setTemp);

                #endregion

                #region======WARP================
                setResult = Convert2BSSATZ();
                Set4ITX.Merge(setResult);
                setResult = Convert2BHSATZ();
                Set4ITX.Merge(setResult);
                setResult = Convert2WTSAER();
                Set4ITX.Merge(setResult);
                DataSet setLoom = new DataSet();
                setLoom = GetLoomType(ITXLOOMTYPE);
                string processedb = "";
                foreach (DataRow rlm in setLoom.Tables[0].Rows)
                {
                    string loomtype = rlm["KEY"].ToString();
                    string beamtype = rlm["BTYPE"].ToString();
                    if (processedb.Contains(loomtype.Trim() + "_" + beamtype + ";"))
                        continue;
                    processedb += loomtype.Trim() + "_" + beamtype + ";";

                    setResult = Convert2BSGROE(loomtype, beamtype);
                    Set4ITX.Merge(setResult);
                }
                setResult = Convert2BSFARB();
                Set4ITX.Merge(setResult);
                setResult = Convert2BASTGA();
                Set4ITX.Merge(setResult);
                //setResult = Convert2BASTGAD();
                //Set4ITX.Merge(setResult);
                setTemp = new DataSet();
                if (Set4ITX.Tables.Contains("GSCOLOR"))
                {
                    tbGSCOLOR = Set4ITX.Tables["GSCOLOR"];
                }
                foreach (DataRow dwMaster in Set4ITX.Tables["BASTGA"].Rows)
                {
                    yart = dwMaster["BAGGART"].ToString();
                    yvar = dwMaster["BAGGVAR"].ToString();
                    ycolor = dwMaster["BAGGFARBE"].ToString();
                    ydesc = dwMaster["BAGBEM"].ToString();
                    //GSCART  GSCVAR GSCFARBE
                    if (tbGSCOLOR.Rows.Count > 0)
                    {
                        expression = "GSCART='" + yart + "' AND GSCVAR='" + yvar + "' AND GSCFARBE='" + ycolor + "'";
                        rows = tbGSCOLOR.Select(expression);
                        if (rows.Length > 0)
                            continue;
                    }
                    setResult = Convert2GSCOLOR(yart, yvar, ycolor, ydesc);
                    setTemp.Merge(setResult);
                }
                Set4ITX.Merge(setTemp);

                stage = "K";
                setTemp = new DataSet();

                foreach (DataRow dwMaster in Set4ITX.Tables["BHSATZ"].Rows)
                {
                    art = dwMaster["BHKART"].ToString();
                    var = dwMaster["BHKVAR"].ToString();
                    setFlow = new DataSet();
                    matchid = "JS";
                    setFlow = GetProcessFlow(stage, art, var, color, matchid);
                    if (setFlow.Tables.Count == 0)
                        continue;
                    if (setFlow.Tables[0].Rows.Count == 0)
                        continue;
                    foreach (DataRow dwFlow in setFlow.Tables[0].Rows)
                    {
                        setResult = Convert2APZUORD(stage, art, var, dwFlow);
                        setTemp.Merge(setResult);
                    }
                    setTemp.Merge(setResult);
                }
                Set4ITX.Merge(setTemp);
                #endregion
                #region======YARN================
                stage = "G";
                setTemp = new DataSet();

                if (Set4ITX.Tables.Contains("GSCOLOR"))
                {
                    tbGSCOLOR = Set4ITX.Tables["GSCOLOR"];
                }
                foreach (DataRow dwMaster in tbGSCOLOR.Rows)
                {
                    //GSCART  GSCVAR GSCFARBE
                    //get only one code 
                    yart = dwMaster["GSCART"].ToString();
                    yvar = dwMaster["GSCVAR"].ToString();
                    ycolor = dwMaster["GSCFARBE"].ToString();
                    matchid = "YD";
                    setFlow = new DataSet();
                    setFlow = GetProcessFlow(stage, yart, yvar, ycolor, matchid);
                    if (setFlow.Tables.Count == 0)
                        continue;
                    if (setFlow.Tables[0].Rows.Count == 0)
                        continue;
                    foreach (DataRow dwFlow in setFlow.Tables[0].Rows)
                    {
                        setResult = Convert2APZUORD(stage, yart, yvar, dwFlow);
                        setTemp.Merge(setResult);
                    }

                    matchid = "FLP";
                    setFlow = new DataSet();
                    setFlow = GetProcessFlow(stage, yart, yvar, ycolor, matchid);
                    if (setFlow.Tables.Count == 0)
                        continue;
                    if (setFlow.Tables[0].Rows.Count == 0)
                        continue;
                    foreach (DataRow dwFlow in setFlow.Tables[0].Rows)
                    {
                        setResult = Convert2APZUORD(stage, yart, yvar, dwFlow);
                        setTemp.Merge(setResult);
                    }
                }
                Set4ITX.Merge(setTemp);
                #endregion
                foreach(DataRow row in this.SetJob.Tables["TechMaster"].Rows)
                {
                    _CADPATTERN = row["WSeq"].ToString().Trim();
                    if (_CADPATTERN.Length == 0)
                        continue;
                    _CADPATTERN =( row["FClothNO"].ToString().Trim()+"-"+ row["FColorNO"].ToString().Trim()).PadRight(20) + row["WSeq"].ToString();
                    VWT.JLTPattern("J" + _CADPATTERN); 
                    VWT.JLTPattern("W" + _CADPATTERN); 
                }


                //throw new Exception("有啥问题？");
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                INTEXBL.CheckEvents("A001013", dwIntexStd, ex.Message);
            }
            finally
            {
                Upload2Intex();
            }
        }
        public bool Upload2Intex()
        {
            bool result = true;
            try
            {
                if (m_wintex == null)
                {
                    m_wintex = new JLTCAD2INTEX(OracleDB, proxy);
                }
                m_wintex.ReSet(Set4ITX, SetDISPO);
                result = m_wintex.Execute(1, true);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return result;
        }
        public void WritePFLog(string result, DataRow dwLog)
        {
            try
            {
                dwLog["CAD2ITXSTATUS"] = gb2312.ConvertBack(System.DateTime.Now.ToShortDateString() + ":" + result, null, null, null);
                INTEXDBC.SaveLOGINFO(dwLog);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public void AutoRun()
        {

            try
            {
                while (true)
                {

                    if (JobQueue.Count > 0)
                    {
                        SetJob = JobQueue.Dequeue();
                        ConvertToIntex();
                        TryStartJob();//loadjob 
                    }
                    else
                    {
                        if (!IsAutoRun)
                        {
                            break;
                        }
                        if (!TryStartJob())
                        {
                            CheckNewJob();
                        }
                        double miliseconds = 1000 * 60 * 2;////5minutes    
                        DateTime dat = DateTime.Now.AddMilliseconds(miliseconds);
                        EventDispatcher.Instance.RaiseStatus("下次转换开始时间是:" + dat.ToLongTimeString(), StatusEventType.StopProgress);
                        System.Threading.Thread.Sleep(1000 * 60 * 2);
                    }
                }
            }
            catch (Exception ex)
            {
                IsRun = false;
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public DataSet CAD2ITX(DataRow dwLog)
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            try
            {
                string fInterID = dwLog["FINTERID"].ToString();
                string fcloth = dwLog["FCLOTHNO"].ToString();
                string fcolor = dwLog["FCOLORNO"].ToString();
                string cadkey = dwLog["FCADKEY"].ToString();
                string designer = dwLog["FDESIGNNAME"].ToString();
                if (fcloth.StartsWith("H"))
                {
                    DataRow dwData = INTEXDB.VAFARBEN.NewRow();
                    dwData["VAFMANDANT"] = "00";
                    dwData["VAFART"] = Left(fcloth, 24);
                    dwData["VAFFARBE"] = Left(fcolor, 10);
                    dwData["VAFARTBEZ"] = Left((string)gb2312.ConvertBack(designer, typeof(string), null, null), 10);
                    INTEXDBC.UpdateVAFARBEN(dwData, "", 2);
                    EventDispatcher.Instance.RaiseStatus(fcloth + "-" + fcolor + "手织板工艺员已经更新", StatusEventType.StopProgress);
                    return SReturn;
                }
                string filePath = @"C:\IntexReport\Data\JLTCAD\01Temp\CAD" + fInterID.Trim() + "-" + fcloth.Trim() + "-" + fcolor.Trim() + ".XML";
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }
                EventDispatcher.Instance.RaiseStatus(fcloth + "-" + fcolor + "读取CAD数据", StatusEventType.StopProgress);
                SResult = PrepareCADData(fInterID, fcloth, fcolor, cadkey);
                if (SResult.Tables.Count > 0)
                {
                    SResult.WriteXml(filePath);
                    SReturn.Merge(SResult);
                    TryStartJob();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public DataSet LoadJob()
        {
            DataSet SReturn = new DataSet();
            try
            {
                bool last = true;
                DataSet SResult = GetJob(ref last);
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public DataSet GetJob(ref  bool last)
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();

            try
            {
                string fileDone;
                string filestemp = @"C:\IntexReport\Data\JLTCAD\01Temp";
                string[] files = System.IO.Directory.GetFiles(filestemp);
                foreach (string file in files)
                {
                    fileDone = file.Replace(@"\01Temp\", @"\02Incoming\");
                    if (System.IO.File.Exists(fileDone))
                    {
                        System.IO.File.Delete(fileDone);
                    }
                    System.IO.File.Move(file, fileDone);
                }
                string files2p = @"C:\IntexReport\Data\JLTCAD\02Incoming";
                files = System.IO.Directory.GetFiles(files2p);

                if (files.Length > 0)
                {
                    string file = files[0];
                    SResult.ReadXml(file);
                    SReturn.Merge(SResult);
                    fileDone = file.Replace(@"\02Incoming\", @"\03Done\");
                    if (System.IO.File.Exists(fileDone))
                    {
                        System.IO.File.Delete(fileDone);
                    }
                    System.IO.File.Move(file, fileDone);
                }
                if (files.Length == 1)
                    last = true;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public bool TryStartJob()
        {
            bool HasBuffer = false;
            DataSet Set = LoadJob();
            if (Set.Tables.Count > 0)
            {
                HasBuffer = true;
                JobQueue.Enqueue(Set);
            }
            else
            {
                return HasBuffer;
            }
            if (!IsRun)
            {
                bgWorker.RunWorkerAsync();
            }
            return HasBuffer;
        }
        public string ReadLog()
        {
            string result = "180660";
            result = "999999";
            try
            {
                DataSet SResult = INTEXDBC.QueryLOGINFO(null, 2);
                foreach (DataRow row in SResult.Tables[0].Rows)
                {
                    result = row["FINTERID"].ToString();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return result;
        }
        public DataSet CheckNewJob()
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            if (!IsRun)
            {
                bgWorker.RunWorkerAsync();
                return SReturn;
            }
            if (SetData.LOGINFO.Rows.Count == 0)
            {
                SetData.Clear();
                SResult = QueryLogInfor(ReadLog(), "");
                SReturn.Merge(SResult);
                SetData.Merge(SResult);
            }
            else
            {
                foreach (DataRow dwLog in SetData.LOGINFO.Rows)
                {
                    if (!IsAutoRun)
                        break;
                    CAD2ITX(dwLog);
                    WritePFLog("", dwLog);
                }
                SetData.Clear();
            }
            if (!IsRun)
            {
                bgWorker.RunWorkerAsync();
            }

            return SReturn;
        }

     
    }
}